﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager : MonoBehaviour {

    public delegate void EventHandler(object args);

    Dictionary<int, EventHandler> m_Evnets = new Dictionary<int, EventHandler>();

    //订阅事件
    public void Subscribe(int id,EventHandler e)
    {
        if (m_Evnets.ContainsKey(id))
            m_Evnets[id] += e;
        else
            m_Evnets.Add(id, e);
    }

    //取消订阅
    public void UnSubscribe(int id, EventHandler e)
    {
        if(m_Evnets.ContainsKey(id))
        {
            if (m_Evnets[id] != null)
                m_Evnets[id] -= e;

            if (m_Evnets[id] == null)
                m_Evnets.Remove(id);
        }      
    }

    public void Fire(int id,object args = null)
    {
        EventHandler handler;
        if(m_Evnets.TryGetValue(id,out handler))
        {
            handler(args);
        }
    }
}
